home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / non-internet / samba / source / nmblookup.c < prev    next >
C/C++ Source or Header  |  1996-06-26  |  5KB  |  218 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    NBT client - used to lookup netbios names
  5.    Copyright (C) Andrew Tridgell 1994-1995
  6.    
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.    
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.    
  21. */
  22.  
  23. #ifdef SYSLOG
  24. #undef SYSLOG
  25. #endif
  26.  
  27. #include "includes.h"
  28. #include "nameserv.h"
  29.  
  30. extern int DEBUGLEVEL;
  31.  
  32. extern pstring scope;
  33.  
  34. extern struct in_addr bcast_ip;
  35. extern pstring myhostname;
  36.  
  37. static BOOL got_bcast = False;
  38.  
  39. int ServerFD= -1;
  40.  
  41. /****************************************************************************
  42.   open the socket communication
  43.   **************************************************************************/
  44. static BOOL open_sockets(void)
  45. {
  46.   struct hostent *hp;
  47.  
  48.   /* get host info */
  49.   if ((hp = Get_Hostbyname(myhostname)) == 0) 
  50.     {
  51.       DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
  52.       return False;
  53.     }   
  54.  
  55.   ServerFD = open_socket_in(SOCK_DGRAM, 0,3);
  56.  
  57.   if (ServerFD == -1)
  58.     return(False);
  59.  
  60.   set_socket_options(ServerFD,"SO_BROADCAST");
  61.  
  62.   DEBUG(3, ("Socket opened.\n"));
  63.   return True;
  64. }
  65.  
  66.  
  67. /****************************************************************************
  68.   initialise connect, service and file structs
  69. ****************************************************************************/
  70. static BOOL init_structs(void )
  71. {
  72.   struct in_addr myip;
  73.  
  74.   if (!get_myname(myhostname,&myip))
  75.     return(False);
  76.  
  77.   /* Read the broadcast address from the interface */
  78.   {
  79.     struct in_addr ip0,ip2;
  80.  
  81.     ip0 = myip;
  82.  
  83.     if (!got_bcast) {
  84.       get_broadcast(&ip0,&bcast_ip,&ip2);
  85.  
  86.       DEBUG(2,("Using broadcast %s\n",inet_ntoa(bcast_ip)));
  87.     }
  88.   }
  89.  
  90.   return True;
  91. }
  92.  
  93. /****************************************************************************
  94. usage on the program
  95. ****************************************************************************/
  96. static void usage(void)
  97. {
  98.   printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
  99.   printf("Version %s\n",VERSION);
  100.   printf("\t-d debuglevel         set the debuglevel\n");
  101.   printf("\t-B broadcast address  the address to use for broadcasts\n");
  102.   printf("\t-M                    searches for a master browser\n");
  103.   printf("\t-S                    lookup node status as well\n");
  104.   printf("\n");
  105. }
  106.  
  107.  
  108. /****************************************************************************
  109.   main program
  110. ****************************************************************************/
  111. int main(int argc,char *argv[])
  112. {
  113.   int opt;
  114.   unsigned int lookup_type = 0x20;
  115.   pstring lookup;
  116.   extern int optind;
  117.   extern char *optarg;
  118.   BOOL find_master=False;
  119.   BOOL find_status=False;
  120.   int i;
  121.   
  122.   DEBUGLEVEL = 1;
  123.   *lookup = 0;
  124.  
  125.   TimeInit();
  126.  
  127.   setup_logging(argv[0],True);
  128.  
  129.   charset_initialise();
  130.  
  131.   while ((opt = getopt(argc, argv, "p:d:B:i:SMh")) != EOF)
  132.     switch (opt)
  133.       {
  134.       case 'B':
  135.     {
  136.       unsigned long a = interpret_addr(optarg);
  137.       putip((char *)&bcast_ip,(char *)&a);
  138.       got_bcast = True;
  139.     }
  140.     break;
  141.       case 'i':
  142.     strcpy(scope,optarg);
  143.     strupper(scope);
  144.     break;
  145.       case 'M':
  146.     find_master = True;
  147.     break;
  148.       case 'S':
  149.     find_status = True;
  150.     break;
  151.       case 'd':
  152.     DEBUGLEVEL = atoi(optarg);
  153.     break;
  154.       case 'h':
  155.     usage();
  156.     exit(0);
  157.     break;
  158.       default:
  159.     usage();
  160.     exit(1);
  161.       }
  162.  
  163.   if (argc < 2) {
  164.     usage();
  165.     exit(1);
  166.   }
  167.  
  168.   init_structs();
  169.   if (!open_sockets()) return(1);
  170.  
  171.   DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_ip)));
  172.  
  173.  
  174.   for (i=optind;i<argc;i++)
  175.     {
  176.       BOOL bcast = True;
  177.       int retries = 2;
  178.       char *p;
  179.       struct in_addr ip;
  180.  
  181.       strcpy(lookup,argv[i]);
  182.  
  183.       if (find_master) {
  184.     if (*lookup == '-') {
  185.       strcpy(lookup,"\01\02__MSBROWSE__\02");
  186.       lookup_type = 1;
  187.     } else {
  188.       lookup_type = 0x1d;
  189.     }
  190.       }
  191.  
  192.       p = strchr(lookup,'#');
  193.  
  194.       if (p) {
  195.     *p = 0;
  196.     sscanf(p+1,"%x",&lookup_type);
  197.     bcast = False;
  198.     retries = 1;
  199.       }
  200.  
  201.       if (name_query(ServerFD,lookup,lookup_type,bcast,True,
  202.              bcast_ip,&ip,NULL)) 
  203.     {
  204.       printf("%s %s\n",inet_ntoa(ip),lookup);
  205.       if (find_status) 
  206.         {
  207.           printf("Looking up status of %s\n",inet_ntoa(ip));
  208.           name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
  209.           printf("\n");
  210.         }
  211.       } else {
  212.     printf("couldn't find name %s\n",lookup);
  213.       }
  214.     }
  215.  
  216.   return(0);
  217. }
  218.